import requests
from flask import Flask, render_template, Response, make_response, jsonify, url_for, redirect, request

from apps.controller.UserController import user_bp
from apps.pojo.Entity import db
from apps.service.LoginService import LoginService

app = Flask(__name__)
app.register_blueprint(user_bp)


app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+pymysql://root:250250@localhost:3306/flaskdemo"
with app.app_context():
    db.init_app(app)
    # db.drop_all()
    db.create_all()
@app.route('/')
def hello_world():  # put application's code here
    result = LoginService.login("root","root")
    print(result.id)  # 打印查询结果
    return '<h1>Hello World!</h1>'

@app.route('/main')
def main():
    return render_template('wor.html')

@app.route('/m')
def m():
    res=Response(response='{"message":"Hello, World!"}',mimetype='application/json')
    return render_template('wor.html')

@app.route('/mm')
def mm():
    user1={"name":"John","age":30}
    user2={"name":"Jane","age":25}
    users=[user1,user2]
    res=make_response(jsonify(users))
    return res
@app.route('/hh/<id>',methods=['GET'])
def hhh(id):
    return render_template('wor.html',id=id)

@app.route('/uiop/loo/<name>')
def mmm(name):
    return f"{url_for('mmm',name=name)}"

#重定向
@app.route('/redirect')
def greet():
    return redirect(url_for('mm'),302)

@app.route('/login')
def g():
    return render_template('login_v2.html')

@app.route('/dologin',methods=['POST'])
def dologin():
    # param=request.form
    # username=param.get('username')
    # password = param.get('password')
    username = request.form['username']
    password = request.form['password']
    result=LoginService.login(username,password)
    if result:
      return render_template('index.html')
    else:
        return render_template('login_v2.html')
    # username = request.form['username']
    # password = request.form['password']
    # print(f"username:{username},password:{password}")
    # return redirect(url_for('index'),302)

#设置数据库连接
# db = {
#     'host': 'localhost',
#     'username': 'root',
#     'password': '250250',
#     'port': 3306,
#     'db': 'flaskdemo'
# }

if __name__ == '__main__':
    app.run()
